package com.holly.unit.bpmn.designer.core.analysis;

import com.holly.unit.bpmn.designer.core.bpmn.BpmnModelActImpl;
import com.holly.unit.bpmn.designer.core.bpmn.BpmnModelFactory;
import com.holly.unit.bpmn.designer.core.strategy.CreateTaskStrategy;
import com.holly.unit.bpmn.designer.core.strategy.CreateTaskStrategyFactory;
import com.holly.unit.bpmn.designer.model.ChildNode;
import org.activiti.bpmn.model.UserTask;

import java.util.ArrayList;
import java.util.List;
/**
 * 类描述: 审批节点解析
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/5/12 11:27
 */
public class SpNodeAnalysis {

    private BpmnModelFactory factory;

    /***
     * 私有构造方法，禁止用户构造对象
     */
    private SpNodeAnalysis() {
        this.factory = new BpmnModelActImpl();
    }

    /***
     * 内部类持有单例实例，只有在被第一次被使用时才会被初始化
     */
    private static class SpNodeAnalysisHolder{
        /**
         * 静态实例变量保证全局唯一实例,被静态内部类持有
         * (静态内部类在外部类初始化不会被初始化，只有在第一次访问是才会被初始化)
         */
        private static SpNodeAnalysis instance = new SpNodeAnalysis();
    }

    public static SpNodeAnalysis getInstance() {
        return SpNodeAnalysisHolder.instance;
    }

    public List<UserTask> createUserTask(List<ChildNode> approverNodes) {
        List<UserTask> tasks = new ArrayList<>();
        for (ChildNode node:approverNodes) {
            String type = node.getProperties().getAssigneeType();
            CreateTaskStrategy createTaskStrategy = CreateTaskStrategyFactory.getCreateTaskStrategy(type);
            UserTask task = createTaskStrategy.createTask(node, factory);
            tasks.add(task);
        }
        return tasks;

    }
}
